/*
* Created by IntelliJ IDEA.
  User: 杨成森
  Date: 2021/8/5
  Time: 21:42
  To change this template use File | Settings | File Templates.
* */

package com.baizhi.action;
import com.baizhi.entity.*;
import com.baizhi.entity.Class;
import com.baizhi.service.*;
import com.baizhi.service.impl.*;
import com.baizhi.util.DateUtil;
import com.opensymphony.xwork2.ActionSupport;

import java.util.List;
import static com.baizhi.dao.StudentDao.PAGE_SIZE;

public class StudentAction extends ActionSupport {

    /*定义成员变量接受数据传递数据*/
    private Integer id;
    private Student student ;
    private List<Student> students;

    private Integer maxNumber;//总条数
    private Integer pageIndex=1;//页码
    private Integer maxPageIndex;//最大页码

    private List<Tag> tags;  //遍历的所有标签
    private String[] tagIds;  //在页面传输过来的标签id值

    private List<City> cities;
    private List<Group> groups;
    private List<Class> clazzes;

    /*模糊查询*/
    private String column;
    private String value;

    private String clazzAndGroup;  //组、班级id



    /*查询一条数据 */
    public String queryStudent(){
        StudentService ss =new StudentServiceImpl();
        student = ss.queryOne(id);

        tagService ts = new TagServiceImpl();
        tags = ts.queryAllTagByType("学生");
        CityService cs = new CityServiceImpl();
        cities = cs.queryAll();
        GroupService gs = new GroupServiceImpl();
        groups = gs.queryAll();

        /* 拿到小组人数*/
        for (Group group : groups) {
            group.setSize(ss.queryNumberByGroupId(group.getId()));
        }

        return "queryonesuccess";
    }


    /* 更新 */
    public String updateStudent(){
        //调用service
        StudentService ss = new StudentServiceImpl();
        String[] names = clazzAndGroup.split("-");
        Group group = new Group();
        group.setId(Integer.valueOf(names[0]));
        Class clazz = new Class();
        clazz.setId(Integer.valueOf(names[1]));

        student.setGroup(group);
        student.setClazz(clazz);

        ss.changeStudent(student,tagIds);

        return "updatesuccess";
    }



    /*添加*/
    public String add()throws Exception{
        //获取参数
        //调用service
        StudentService ss = new StudentServiceImpl();
        String[] names = clazzAndGroup.split("-");
        Group group = new Group();
        group.setId(Integer.valueOf(names[0]));
        Class clazz = new Class();
        clazz.setId(Integer.valueOf(names[1]));
//        String star = DateUtil.getConstellation(student.getBirthday());
        student.setGroup(group);
        student.setClazz(clazz);

        ss.addStudent(student,tagIds);

        return "addSuccess";
    }


    /* 删除 */
    public String remove()throws Exception{
        //获取参数
        System.out.println(id);
        StudentService ss = new StudentServiceImpl();
        ss.removeStudentById(id);

        return "removeSuccess";
    }



    /*查询所有*/
    public String queryAll()throws Exception{
        //调用service
        StudentService ss = new StudentServiceImpl();
        /*查询到的总人数*/
        maxNumber = ss.getCountByLike(column,value);
        students = ss.queryAllStudentByPage(column,value,pageIndex);

        for (Student student1 : students) {
            tags = student1.getTags();
        }

        maxPageIndex=maxNumber%PAGE_SIZE==0?maxNumber/PAGE_SIZE:maxNumber/PAGE_SIZE+1;

        //跳转
        return "queryAllSuccess";
    }



    /*  查询学生对应的标签   添加学生用到*/
    public String queryTags()throws Exception{
        //接受数据
        //调用service
        // 查询学生对应的标签
        tagService ts = new TagServiceImpl();
        tags = ts.queryAllTagByType("学生");
        CityService cs = new CityServiceImpl();
        cities = cs.queryAll();
        GroupService gs = new GroupServiceImpl();
        groups = gs.queryAll();
        StudentService ss = new StudentServiceImpl();

        /* 遍历查询小组人数  */
        for (Group group : groups) {
            group.setSize(ss.queryNumberByGroupId(group.getId()));
        }

        //跳转
        return "queryTagsSuccess";
    }




    //get/set
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public Integer getMaxNumber() {
        return maxNumber;
    }

    public void setMaxNumber(Integer maxNumber) {
        this.maxNumber = maxNumber;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getMaxPageIndex() {
        return maxPageIndex;
    }

    public void setMaxPageIndex(Integer maxPageIndex) {
        this.maxPageIndex = maxPageIndex;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

    public List<City> getCities() {
        return cities;
    }

    public void setCities(List<City> cities) {
        this.cities = cities;
    }

    public List<Group> getGroups() {
        return groups;
    }

    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }

    public List<Class> getClazzes() {
        return clazzes;
    }

    public void setClazzes(List<Class> clazzes) {
        this.clazzes = clazzes;
    }



    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getClazzAndGroup() {
        return clazzAndGroup;
    }

    public void setClazzAndGroup(String clazzAndGroup) {
        this.clazzAndGroup = clazzAndGroup;
    }

    public String[] getTagIds() {
        return tagIds;
    }

    public void setTagIds(String[] tagIds) {
        this.tagIds = tagIds;
    }
}
